<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis SETNX 命令原子性深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .image-container {
            position: relative;
            overflow: hidden;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .image-container img {
            transition: transform 0.3s ease;
        }
        
        .image-container:hover img {
            transform: scale(1.05);
        }
        
        .mermaid {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.6s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center animate-fadeInUp">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">
                    Redis SETNX 命令
                </h1>
                <p class="text-2xl md:text-3xl font-light mb-8 opacity-90">
                    原子性深度解析
                </p>
                <div class="flex justify-center space-x-4 text-lg">
                    <span class="flex items-center">
                        <i class="fas fa-lock mr-2"></i>
                        分布式锁
                    </span>
                    <span class="flex items-center">
                        <i class="fas fa-atom mr-2"></i>
                        原子操作
                    </span>
                    <span class="flex items-center">
                        <i class="fas fa-shield-alt mr-2"></i>
                        并发安全
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="max-w-5xl mx-auto">
            
            <!-- Introduction -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover animate-fadeInUp">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">S</span>ETNX（Set if Not eXists）命令是Redis中的一个原子性命令，它用于设置一个键的值，但仅在该键不存在时才执行设置。这个看似简单的命令，在分布式系统中扮演着至关重要的角色，特别是在实现分布式锁等场景中。
                </p>
            </div>

            <!-- Visual Diagram -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 animate-fadeInUp">
                <h3 class="text-2xl font-bold mb-6 text-center text-gradient">SETNX 工作原理图解</h3>
                <div class="mermaid">
                    graph TD
                        A[客户端发送 SETNX 命令] --> B{键是否存在?}
                        B -->|不存在| C[设置键值]
                        B -->|已存在| D[返回 0]
                        C --> E[返回 1]
                        E --> F[获得锁/操作成功]
                        D --> G[未获得锁/操作失败]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style B fill:#f59e0b,stroke:#fff,stroke-width:2px,color:#fff
                        style C fill:#10b981,stroke:#fff,stroke-width:2px,color:#fff
                        style D fill:#ef4444,stroke:#fff,stroke-width:2px,color:#fff
                        style E fill:#10b981,stroke:#fff,stroke-width:2px,color:#fff
                        style F fill:#3b82f6,stroke:#fff,stroke-width:2px,color:#fff
                        style G fill:#6b7280,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>

            <!-- Redis Single Thread Model -->
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-2xl shadow-xl p-8 card-hover animate-fadeInUp">
                    <h2 class="text-2xl font-bold mb-4 section-title">
                        <i class="fas fa-microchip mr-3 text-purple-600"></i>
                        Redis单线程模型
                    </h2>
                    <p class="text-gray-700 leading-relaxed mb-4">
                        Redis采用单线程模型处理客户端请求，每个命令都在一个原子性的上下文中执行。这种设计保证了SETNX命令的原子性，即在检查键是否存在和设置新值之间没有其他操作干扰。
                    </p>
                    <div class="bg-purple-50 rounded-lg p-4 mt-4">
                        <p class="text-sm text-purple-800">
                            <i class="fas fa-info-circle mr-2"></i>
                            单线程模型避免了多线程编程中的锁竞争和上下文切换开销，使得Redis能够高效地处理大量并发请求。
                        </p>
                    </div>
                </div>
                
                <div class="bg-white rounded-2xl shadow-xl p-8 card-hover animate-fadeInUp" style="animation-delay: 0.1s">
                    <h2 class="text-2xl font-bold mb-4 section-title">
                        <i class="fas fa-atom mr-3 text-blue-600"></i>
                        原子性操作的定义
                    </h2>
                    <p class="text-gray-700 leading-relaxed mb-4">
                        SETNX是一个原子性操作，这意味着它是不可分割的。在执行SETNX命令的整个过程中，不会被其他客户端的命令插入，保证了整个操作的原子性。
                    </p>
                    <div class="bg-blue-50 rounded-lg p-4 mt-4">
                        <p class="text-sm text-blue-800">
                            <i class="fas fa-shield-alt mr-2"></i>
                            原子性是并发编程中的核心概念，确保操作要么完全执行，要么完全不执行。
                        </p>
                    </div>
                </div>
            </div>

            <!-- Execution Steps -->
            <div class="bg-gradient-to-r from-purple-600 to-blue-600 rounded-2xl shadow-xl p-1 mb-12 animate-fadeInUp">
                <div class="bg-white rounded-2xl p-8">
                    <h2 class="text-3xl font-bold mb-6 text-center text-gradient">
                        <i class="fas fa-tasks mr-3"></i>
                        执行的两个步骤
                    </h2>
                    <div class="grid md:grid-cols-2 gap-6 mt-8">
                        <div class="bg-gray-50 rounded-xl p-6 text-center">
                            <div class="text-4xl text-purple-600 mb-4">
                                <i class="fas fa-search"></i>
                            </div>
                            <h3 class="text-xl font-bold mb-2">步骤一：检查</h3>
                            <p class="text-gray-700">检查指定的键是否已经存在于Redis数据库中</p>
                        </div>
                        <div class="bg-gray-50 rounded-xl p-6 text-center">
                            <div class="text-4xl text-blue-600 mb-4">
                                <i class="fas fa-edit"></i>
                            </div>
                            <h3 class="text-xl font-bold mb-2">步骤二：设置</h3>
                            <p class="text-gray-700">如果键不存在，则设置新值；否则保持原值不变</p>
                        </div>
                    </div>
                    <p class="text-center mt-6 text-gray-600">
                        <i class="fas fa-lock mr-2"></i>
                        这两个步骤作为一个整体原子执行，中间不会被其他操作打断
                    </p>
                </div>
            </div>

            <!-- Distributed Lock Application -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover animate-fadeInUp">
                <h2 class="text-2xl font-bold mb-6 section-title">
                    <i class="fas fa-network-wired mr-3 text-green-600"></i>
                    分布式锁应用
                </h2>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-green-50 rounded-lg p-6 text-center">
                        <div class="text-3xl text-green-600 mb-3">
                            <i class="fas fa-lock"></i>
                        </div>
                        <h4 class="font-bold mb-2">获取锁</h4>
                        <p class="text-sm text-gray-700">使用SETNX尝试设置锁键</p>
                    </div>
                    <div class="bg-yellow-50 rounded-lg p-6 text-center">
                        <div class="text-3xl text-yellow-600 mb-3">
                            <i class="fas fa-clock"></i>
                        </div>
                        <h4 class="font-bold mb-2">设置过期</h4>
                        <p class="text-sm text-gray-700">为锁设置过期时间防止死锁</p>
                    </div>
                    <div class="bg-red-50 rounded-lg p-6 text-center">
                        <div class="text-3xl text-red-600 mb-3">
                            <i class="fas fa-unlock"></i>
                        </div>
                        <h4 class="font-bold mb-2">释放锁</h4>
                        <p class="text-sm text-gray-700">操作完成后删除锁键</p>
                    </div>
                </div>
                <div class="mt-6 p-4 bg-gray-100 rounded-lg">
                    <code class="text-sm">
                        <span class="text-purple-600">SETNX</span> lock:resource:123 "client-id" <br>
                        <span class="text-purple-600">EXPIRE</span> lock:resource:123 30
                    </code>
                </div>
            </div>

            <!-- Race Condition Prevention -->
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-2xl shadow-xl p-8 card-hover animate-fadeInUp">
                    <h2 class="text-2xl font-bold mb-4 section-title">
                        <i class="fas fa-shield-virus mr-3 text-red-600"></i>
                        防止竞态条件
                    </h2>
                    <p class="text-gray-700 leading-relaxed mb-4">
                        在多个客户端并发地尝试设置相同的键时，使用SETNX可以防止竞态条件。只有一个客户端能够成功设置新值，其他客户端将在键已存在时放弃设置。
                    </p>
                    <div class="bg-red-50 rounded-lg p-4">
                        <p class="text-sm text-red-800">
                            <i class="fas fa-